//
//  VoiceRecordButton.m
//  YunShouHu
//
//  Created by WangQiang on 16/6/24.
//  Copyright © 2016年 WangQiang. All rights reserved.
//

#import "VoiceRecordButton.h"
#import "VoiceRecordLoadingView.h"
#import "WQVoiceTool.h"

#import <AVFoundation/AVFoundation.h>
@interface VoiceRecordButton()
@property (weak ,nonatomic) VoiceRecordLoadingView *loadingView;
@property (copy ,nonatomic) NSString *recordName;
@property (assign ,nonatomic) BOOL hasRecord;
@end
@implementation VoiceRecordButton
-(void)recordCommonInit{
    [self addTarget:self action:@selector(startRecord) forControlEvents:UIControlEventTouchDown];
    [self addTarget:self action:@selector(stopRecord) forControlEvents:UIControlEventTouchUpInside];
//    [self addTarget:self action:@selector(cancelRecord) forControlEvents:UIControlEventTouchCancel];
    [self addTarget:self action:@selector(cancelRecord) forControlEvents:UIControlEventTouchUpOutside];
//    [self addTarget:self action:@selector(cancelRecord) forControlEvents:UIControlEventTouchDragOutside];
//    UIImage *image = [UIImage imageNamed:@"bg_measurement.9"];
//    image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height *0.5, image.size.width *0.5, image.size.height *0.5, image.size.width *0.5) resizingMode:UIImageResizingModeTile];
//    [self setBackgroundImage:image forState:UIControlStateNormal];
//    UIImage *hilightedImage = [UIImage imageNamed:@"btn_blue_bg_disabled.9"];
//    hilightedImage = [hilightedImage resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height *0.5, image.size.width *0.5, image.size.height *0.5, image.size.width *0.5) resizingMode:UIImageResizingModeTile];
//    [self setBackgroundImage:hilightedImage forState:UIControlStateHighlighted];
    self.backgroundColor = [UIColor lightGrayColor] ;
    [self addCornerRadius:5.0];
    self.layer.borderWidth = 0.5;
    self.layer.borderColor = [UIColor whiteColor].CGColor;
    [self setTitle:@"按住说话" forState:UIControlStateNormal];
    [self setTitle:@"正在录音..." forState:UIControlStateHighlighted];
    [self initRecorder];
}
-(void)initRecorder{
    self.recordName = [NSString stringWithFormat:@"%@.wav",[NSDate date]];
   NSError *error = [[WQVoiceTool sharedInstance] recordWithName:self.recordName];
    
    if(error){
        NSLog(@"===%@",error);
    }
}
-(instancetype)initVocieRecordButton{
    if(self = [super init]){
        [self recordCommonInit];
    }
    return self;
}
-(void)dealloc{
    if(!self.hasRecord){
        
    }
}
-(void)setHighlighted:(BOOL)highlighted{
    if(!highlighted){
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [super setHighlighted:highlighted];
            self.backgroundColor = [UIColor lightGrayColor] ;
        });
    }else{
     
        [super setHighlighted:highlighted];
        self.backgroundColor = [UIColor darkGrayColor];
    }
   
}
-(void)sendData{
//    if([self.delegate respondsToSelector:@selector(voiceRecordFinshRecord:recordData:duration:error:)]){
//        NSString *path = [[VoiceTool sharedVoice].cachePath stringByAppendingPathComponent:self.recordName];
//        NSData *recordData = [NSData dataWithContentsOfFile:path];
//        NSError *error;
//        AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithData:recordData error:&error];
////        [VoiceTool saveData:recordData path:self.recordName];
//        recordData =   [VoiceTool wavToAmr:recordData];
//        if(!error && audio.duration > 2.0){
//            [self.delegate voiceRecordFinshRecord:self.recordName recordData:recordData duration:audio.duration error:nil];
//        }else{
//            [[VoiceTool sharedVoice] cancelRecord];
//            if(self.loadingView){
//                [self.loadingView hideWithCompeletion:NULL];
//            }
//            
//            if(!error && audio.duration <= 2.0){
//                error = [HttpTool errorWithErrorMessage:NSLocalizedString(@"录音时间太短", nil)];
//            }
//           
//             [self.delegate voiceRecordFinshRecord:self.recordName recordData:recordData duration:audio.duration error:error];
//        }
//    }
}
//-(void)stopRecord{
//    [[VoiceTool sharedVoice] stopRecord];
//    if(self.loadingView){
//        typeof(self) weakSelf = self;
//        [self.loadingView hideWithCompeletion:^(BOOL finsh) {
//            [weakSelf sendData];
//        }];
//    }else{
//        [self sendData];
//    }
//}
//-(void)startRecord{
//    self.hasRecord = YES;
//    VoiceRecordLoadingView *loadingView = [[VoiceRecordLoadingView alloc] initWithFrame:CGRectMake(0, 0, APP_WIGHT, APP_HEIGHT - 100)];
//    self.loadingView = loadingView;
//    [loadingView show];
//    [[VoiceTool sharedVoice] startRecord];
//}
//-(void)cancelRecord{
//    [[VoiceTool sharedVoice] cancelRecord];
//    if(self.loadingView){
//        [self.loadingView hideWithCompeletion:^(BOOL finsh) {
//            [MBProgressHUD showTipMessage:NSLocalizedString(@"取消了录音", nil)];
//        }];
//    }
//}

@end
